@isTest
private class PartsLendingApplicationUtilTest {
    static testMethod void testMethod1() {


    	Id ldcProfile = [Select Id From Profile Where Name like '%工程师%' limit  1  ].Id;
    	Id ldcProfile1 = [Select Id From Profile Where Name like '%工程部主管%' limit  1  ].Id;

        



         User u2 = new User( Alias = 'samLdc2', 
                         Email = 'samLdc2@test.com.maike',
                         Emailencodingkey = 'UTF-8', 
                         Lastname = '用户1', 
                         Languagelocalekey = 'en_US',
                         Localesidkey = 'en_US', 
                         Profileid = ldcProfile1, 
                         Country = 'United States',
                         IsActive = true,
                         CompanyName= 'Y-MKSY',
                         SubCompany__c='四川省迈克实业有限公司',
                         UserDepartment__c='客户服务中心(工程部)',
                         Timezonesidkey = 'America/Los_Angeles', 
                         EmployeeNumber = '123452',
                         Username = 'Matldc2@test.com.maike',
                         FederationIdentifier ='123452', 
                         Province__c = '四川省',
                         ManagerId = UserInfo.getUserId(),
                         SubCompanyManager__c = UserInfo.getUserId(),
                         ResponseCode__c = 'TTTT' );
         insert u2; 

          User u1 = new User( Alias = 'samLdc1', 
                         Email = 'ldc@test.com.maike',
                         Emailencodingkey = 'UTF-8', 
                         Lastname = '用户1', 
                         Languagelocalekey = 'en_US',
                         Localesidkey = 'en_US', 
                         Profileid = ldcProfile, 
                         Country = 'United States',
                         IsActive = true,
                         CompanyName= 'Y-MKSY',
                         SubCompany__c='四川省迈克实业有限公司',
                         UserDepartment__c='客户服务中心(工程部)',
                         Timezonesidkey = 'America/Los_Angeles', 
                         EmployeeNumber = '123451',
                         Province__c = '四川省',
                         Username = 'Matldc1@test.com.maike',
                         FederationIdentifier ='123451',

                         ManagerId=u2.Id , 
                         ResponseCode__c = 'TTTT' );
         insert u1; 

       Account acc=new Account();
    	acc.Name='Test1229J';
    	acc.NCICCode__c = 'xxxxx';
    	acc.AccountNumber = '123456';
    	insert acc;

    	AccountCredit__c acd = new AccountCredit__c();
    	acd.Company__c = '四川省迈克实业有限公司';
    	acd.NCCreditDeadline__c = '0';
    	acd.Account__c = acc.Id;
    	insert acd;

        UserInformation__c uinfo1= new UserInformation__c();
        uinfo1.SubCompany__c = '四川省迈克实业有限公司';
        uinfo1.OrderProcessingAssistant__c = UserInfo.getUserId();
        uinfo1.SalesAssistant__c = UserInfo.getUserId();
        uinfo1.EngineeringAssistant__c = UserInfo.getUserId();
        insert uinfo1; 


    	Case cas=new Case();
    	cas.AccountId=acc.Id;
        cas.Engineer__c = u1.Id ;
    	insert cas;

        Product__c p = TestUtils.createProduct();
        p.Brand__c = '迈克';
        p.Category3__c = '仪器';
        p.Company__c = '四川省迈克实业有限公司';
        p.Category0__c = '仪器';
        p.ProductName__c ='测试配件';
        p.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
        p.NCCode__c = '1001110000';
        insert p;

        Equipment__c e1 = new Equipment__c();
        e1.Account__c = acc.Id;
        e1.IMEINumber__c = '11';
        e1.Product__c = p.Id;
        insert e1;
        
    	
    	WorkOrder__c wo=new WorkOrder__c();
    	wo.Case__c=cas.Id;
    	wo.Account__c=acc.Id;
    	wo.Status__c='处理中';
        wo.Technician__c  = u1.Id ;
        wo.ServiceManufacture__c =e1.Id; 
        wo.InventoryOrganization__c = 'MKSY';
        wo.PartsInvoicingCustomers__c = acc.Id;
        wo.InstallmentSale__c = '是';
        wo.FollowupProcessing__c = '其他出入库';
        wo.DocumentTypes__c = '库存其他出库单';
        wo.PhysicalDelivery__c = '是';
        wo.PartsInvoicingCustomers__c = acc.Id;

    	insert wo;
        


         Product__c p1 = TestUtils.createProduct();
        p1.Brand__c = '迈克';
        p1.Category0__c = '配件';
        p1.ProductName__c ='测试配件';
        p1.Category1__c = '配件';
        p1.Company__c = '四川省迈克实业有限公司';

        p1.Category3__c = '配件';
        p1.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
        p1.NCCode__c = '1101110000';
        insert p1; 

        Price__c pr1 = new Price__c(
            Product__c = p1.Id,
            Company__c = '四川省迈克实业有限公司',
            CompanyBottomPrice__c = 1.0,
            HospitalBottomPrice__c = 1.0,
            InvoiceDealerPrice__c = 1.0,
            AreaDealerPrice__c = 1.0,
            MarketPrice__c = 1.0,
            HospitalManagerPrice__c= 1.0,
            InstCost__c = 0.5
            
        );

        insert pr1;

                     System.Test.startTest();


        PartsInvoiceApplication__c pia = null;
       

            PartsLendingApplication__c pla = new PartsLendingApplication__c();
           // pla.IsSycToNc__c = true;
            pla.InventoryOrganization__c = 'MKSY';
            pla.FollowupProcessing__c = '经销商备用';
            pla.InstallmentSale__c = '是';
            pla.DocumentTypes__c = '库存借出';
            pla.PhysicalDelivery__c = '是';
            pla.InvoicingCustomers__c = acc.Id;
            pla.TerminalClient__c = acc.Id;
            pla.OwnerId  = u1.Id;
            insert pla;

            PartsLendingDetails__c pld = new PartsLendingDetails__c();
            pld.Product__c = p1.Id;
            pld.No__c =5;
            pld.A_InvoiceNo__c = 1;
            pld.A_WarrantyNo__c =1;
            pld.B_No__c = 5;
            pld.PartsLendingApplication__c = pla.Id;
            insert pld;

            System.debug('PartsLendingApplicationUtilTest==>'+PartsLendingApplicationUtil.notiAssitant(pla.Id));
            System.debug('PartsLendingApplicationUtilTest==>'+PartsLendingApplicationUtil.syncToNc(pla.Id));

            PartsReturn__c pr = new PartsReturn__c();
            pr.OwnerId  = u1.Id; 
            pr.Company__c = 'MKSY';
            insert pr;
            PartsReturnDetails__c prd = new PartsReturnDetails__c();
            prd.Parts__c = pld.Id;
            prd.PartsReturn__c = pr.Id;
            prd.ReturnAccessories__c = p1.Id;
            prd.ReturnQuantity__c =1;

            insert prd;

            System.debug('PartsLendingApplicationUtilTest==>'+PartsLendingApplicationUtil.notiAssitant(pr.Id));
            PartsLendingApplicationUtil.getDetails(pr.Id);

            PartsLendingApplicationUtil.comfirmBack(pr.Id);

            Map<String,String> m = new Map<String,String>();
            m.put(pld.Id , 'xc0001');
            m.put('lendNum', '5');
            m.put('outno', 'xc');
            List<Map<String,String> > ml = new List<Map<String,String> >();
            ml.add(m);
            PartsLendingApplicationUtil.backWriteFromNc(ml);

            Map<String,Map<String,String>> mm = new Map<String,Map<String,String>>();
            mm.put(pld.Id , m);
            System.debug('PartsLendingApplicationUtilTest==>'+PartsLendingApplicationUtil.backWriteAllDataFromNc(mm));
            

            PartsLendingApplicationUtil.getDetails(pla.Id);
           
            PartsLendingApplicationUtil.getDetails(wo.Id);
            PartsLendingApplicationUtil.getAllLendings(u1.Id);

            
            WorkOrderProduct__c wop = new WorkOrderProduct__c();

            wop.WorkOrder__c  = wo.Id;
            wop.Product__c = p1.Id;
            wop.PartsLendingDetails__c = pld.Id;
            wop.Quantity__c = 2;
            wop.WarrantyNo__c = 1;
            wop.InvoiceNo__c = 1;
            insert wop;


             pia = new PartsInvoiceApplication__c();
            pia.RelatedWorkOrder__c = wo.Id;
            pia.OwnerId  = u1.Id; 
            pia.InvoiceAccount__c = acc.Id;
            pia.EngineeringSupervisor__c = u2.Id ;
            pia.SalesSupervisor__c = u2.Id;
            pia.InventoryOrganization__c = 'MKSY';
            insert pia;
            

            PartsInvoiceDetails__c piad = new PartsInvoiceDetails__c();
            piad.Parts__c = p1.Id;
            piad.ConfirmationQuantity__c = 1;
            piad.InvoiceAmount_Saler__c = 1;
            piad.PartsInvoiceApplication__c = pia.Id;
            piad.WorkOrederProduct__c = wop.Id;
            insert piad;


              AccessoryWarranty__c aw = new AccessoryWarranty__c();
	         aw.RelatedWorkOrder__c  = wo.Id;
	         aw.InstallmentSale__c = '是';
	         aw.Phone__c = '1';
	         aw.DocumentTypes__c = '库存其他出库单';
	         aw.PhysicalDelivery__c = '是';
	         aw.FollowupProcessing__c = '其他出入库';
	         aw.Status__c = '已批准';
             aw.OwnerId  = u1.Id; 
	         insert aw;

	         AccessoryWarrantyDetails__c awd = new AccessoryWarrantyDetails__c();
	         awd.Parts__c = p1.Id;
	         awd.AccessoryWarranty__c = aw.Id;
	         awd.Name = '测试配件';
	         insert awd ;

	         System.debug('PartsLendingApplicationUtilTest==>'+PartsLendingApplicationUtil.syncAccessoryWarranty(aw.Id));


                 PartsLendingApplicationUtil.getAllDetails();
                 PartsLendingApplicationUtil.backWriteAllFromNc(mm);

           

        

        pia.Status__c = '已批准';
         update pia;
         System.debug('PartsLendingApplicationUtilTest==>'+PartsLendingApplicationUtil.syncInvoiceDetail(pia.Id ));
 		System.debug('PartsLendingApplicationUtilTest==>'+PartsLendingApplicationUtil.notiAssitant(wo.Id));

        // System.debug('PartsLendingApplicationUtilTest==>'+PartsLendingApplicationUtil.backSyncToNc(wo.Id));

       


        
    }
}